<template>
  <div class="category">
    <!-- 搜索 -->
    <div class="search">
      <router-link tag="div" to="/home/search" class="box">
        <van-icon class="icon" name="search" size="14" />
        <div class="info">商品搜索,共239款好物</div>
      </router-link>
    </div>
    <!-- 内容 -->
    <div class="content">
      <!-- 一级分类 -->
      <ul class="left">
        <li class="item" :class="currentTab == index?'active':''" v-for='(item,index) in oneList' :key='item.id' @click="changTab(index)">{{item.name}}</li>
      </ul>
      <!-- 二级分类 -->
      <div class="right">
        <!-- 广告 -->
        <div class="banner">
          <img :src="currentData.banner_url" alt="">
        </div>
        <!-- 标题 -->
        <div class="title">
          <span class="line">—</span>
          <span class="name">{{currentData.name}}分类</span>
          <span class="line">—</span>
        </div>
        <!-- 二级分类 -->
        <div class="list">
          <router-link tag="div" :to="`/category/categoryList?id=${item.id}`" class="item" v-for='item in twoList' :key='item.id'>
            <img :src="item.wap_banner_url" alt="">
            <div class="name">{{item.name}}</div>
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { indexaction, currentaction } from "@/api/category";
export default {
  name: "category",
  data() {
    return {
      oneList: [], // 一级分类
      currentData: "", // 当前数据
      twoList: [], // 二级分类
      currentTab: 0, // 当前分类 0，1，2...
    };
  },
  // 计算
  computed: {},
  // 创建
  created() {
    // 一级分类
    indexaction().then((res) => {
      this.oneList = res.data.categoryList;
      // 二级分类
      currentaction({
        id: this.oneList[this.currentTab].id,
      }).then((res) => {
        this.currentData = res.data.data.currentOne;
        this.twoList = res.data.data.currentOne.subList;
      });
    });
  },
  // 准备
  mounted() {},
  // 方法
  methods: {
    // 一级分类
    changTab(val) {
      this.currentTab = val;
      // 二级分类
      currentaction({
        id: this.oneList[this.currentTab].id,
      }).then((res) => {
        this.currentData = res.data.data.currentOne;
        this.twoList = res.data.data.currentOne.subList;
      });
    },
  },
};
</script>

<style lang='scss' scoped>
.category {
  // 搜索
  .search {
    width: calc(100% - 30px);
    height: 44px;
    padding: 0 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom: 0.8px solid #ededed;
    position: fixed;
    top: 0;
    left: 0;
    background-color: #fff;
    .box {
      width: 100%;
      height: 28px;
      border-radius: 5px;
      background-color: #ededed;
      display: flex;
      align-items: center;
      justify-content: center;
      .icon {
        margin-right: 5px;
      }
    }
  }
  // 内容
  .content {
    display: flex;
    width: 100%;
    position: absolute;
    top: 44px;
    left: 0;
    bottom: 50px;
    background-color: #fff;
    // 一级分类
    .left {
      width: 81px;
      .item {
        font-size: 18px;
        list-style: none;
        line-height: 45px;
        height: 45px;
        color: #333;
        font-size: 14px;
        border-left: 3px solid #fff;
      }
      .active {
        color: #ab2b2b;
        font-size: 20px;
        border-left: 3px solid #ab2b2b;
      }
    }
    // 二级分类
    .right {
      flex: 1;
      padding: 0 15px;
      border-left: 0.8px solid #fafafa;
      .banner {
        margin-top: 10px;
        img {
          width: 100%;
          height: 111px;
          vertical-align: top;
        }
      }
      .title {
        padding: 25px 0;
        .name {
          padding: 0 5px;
        }
        .line {
          color: #999;
        }
      }
      .list {
        display: flex;
        flex-wrap: wrap;
        .item {
          width: calc(100% / 3);
          margin-bottom: 10px;
          img {
            width: 72px;
            height: 72px;
            vertical-align: top;
          }
        }
      }
    }
  }
}
</style>